<%--
  User: Shengzhao Li
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="custom" tagdir="/WEB-INF/tags" %>
<html>
<head>
    <title>搜索结果</title>

</head>
<body>
<div id="thing_overview_page" data-role="page" class="page">
    <div data-role="header" data-theme="f">
        <a href="${contextPath}/m/dashboard" data-icon="home" data-direction="reverse" data-transition="slide">主页</a>

        <h1>搜索结果</h1>
        <a href="../overview" data-icon="arrow-l" data-direction="reverse" data-transition="flip">返回</a>
    </div>
    <div data-role="content">
        <div data-role="controlgroup">
            <a href="javascript:void(0);" data-role="button" data-theme="b" id="research_link"
               data-mini="true">重新搜索</a>
            根据关键字 <strong>${thingSearchDto.key}</strong> 搜索到<strong class="red">${thingSearchDto.fullListSize}</strong>条结果.
        </div>
        <ul data-role="listview" id="thing_search_view" data-inset="true">
            <c:forEach items="${thingSearchDto.list}" var="t">
                <li ${t.showWarning?'data-theme="e"':''}>
                    <c:if test="${t.allowAction}" var="allowAction">
                        <a href="action/${t.guid}?key=${thingSearchDto.key}">
                            <custom:thing_search_result_tag thing="${t}"/>
                        </a>
                    </c:if>
                    <c:if test="${not allowAction}">
                        <custom:thing_search_result_tag thing="${t}"/>
                    </c:if>
                </li>
            </c:forEach>
            <c:if test="${empty thingSearchDto.list}">
                <li>
                    搜索结果为空.
                </li>
            </c:if>
        </ul>
        <c:if test="${thingSearchDto.showMore}">
            <div data-role="controlgroup" id="search_more_div">
                <a href="javascript:void(0);" id="search_more_link" data-role="button" class="color-b">
                    更多 (<span
                        id="page_number">${thingSearchDto.pageNumber}</span>/${thingSearchDto.totalPages})<img
                        id="more_loading" src="${contextPath}/images/public/load.gif" alt="loading" class="more-img"/>
                </a>
            </div>
        </c:if>
    </div>
    <script type="text/javascript">
        $(function() {
            $("a#research_link").bind("click", function() {
                $.mobile.changePage("initial", {data:{'key':'${thingSearchDto.key}'}});
                return false;
            });

            var pageNumber = ${thingSearchDto.pageNumber};
            var totalSize = ${thingSearchDto.totalSize};
            var key = '${thingSearchDto.key}';

            $("a#search_more_link").bind("click", function() {
                var $this = $(this).addClass("gray");
                var nextPageNumber = (++pageNumber);
                var loading = $("#more_loading").show();
                getData();
                function getData() {
                    $.ajax({
                        url:"load/more",
                        data:{
                            pageNumber: nextPageNumber,
                            key:key,
                            totalSize: totalSize
                        },
                        success:function(data) {
                            var listview = $("#thing_search_view").append($.trim(data)).listview("refresh");
                            $this.removeClass("gray");
                            checkShowMore(listview);
                        }
                    });
                }

                function checkShowMore(listview) {
                    var addedSize = listview.find("li").size();
                    if (addedSize == totalSize) {
                        $("#search_more_div").hide();
                    } else {
                        loading.hide();
                    }
                    $this.find("#page_number").html(nextPageNumber);
                }
            });
        });
    </script>
</div>
</body>
</html>